package com.xnyzc.lhy.didi.mq;

import com.alibaba.fastjson.JSON;
import com.xnyzc.lhy.common.component.config.security.RocketConfig;
import com.xnyzc.lhy.common.component.redis.RedisCommon;
import com.xnyzc.lhy.common.constant.MqConstant;
import com.xnyzc.lhy.common.constant.MqConsumerGroupConstant;
import com.xnyzc.lhy.common.constant.RedisPrefixConstant;
import com.xnyzc.lhy.common.entity.Qw;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.penum.order.EOrderChannelType;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.didi.controller.synchronization.OrderStatusSyncController;
import com.xnyzc.lhy.didi.entity.order.OaCOrderPersonal;
import com.xnyzc.lhy.didi.entity.param.xiecheng.request.ConfirmOrderRequest;
import com.xnyzc.lhy.didi.mapper.sys.OaSysOfficeMapper;
import com.xnyzc.lhy.didi.service.tongcheng.TcOrderStatusSyncService;
import com.xnyzc.lhy.didi.service.xiecheng.XcOrderStatusSyncService;
import com.xnyzc.lhy.order.entity.gps.OaDVehicleGps;
import com.xnyzc.lhy.order.entity.sidebar.OaSysOffice;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class MQConsumer {
    /**
     * rocketMq 配置文件
     */
    @Autowired
    RocketConfig rocketConfig;

    @Autowired
    private OaSysOfficeMapper oaSysOfficeMapper;

    private DefaultMQPushConsumer consumer;

    @Autowired
    private TcOrderStatusSyncService tcOrderStatusSyncService;

    @Autowired
    private XcOrderStatusSyncService xcOrderStatusSyncService;


//    @Value("${rocketMq.isStart}")
//    private boolean isStart;

    /**
     * 开启监听
     */
    @PostConstruct
    public void init() {
        log.info(rocketConfig.getTopic() + "--------------------------------------------------" + rocketConfig.getNameServerAddr());
//        if (!isStart) {
//            return;
//        }
        try {
            consumer = new DefaultMQPushConsumer(MqConsumerGroupConstant.BIND_DRIVER);
            consumer.setNamesrvAddr(rocketConfig.getNameServerAddr());
            consumer.subscribe(rocketConfig.getTopic(), MqConstant.BIND_DEFAULT_DRIVER);
            //批量消费的最大消息条数
            consumer.setConsumeMessageBatchMaxSize(10);
            //设置消费策略:队列头消费
            consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
            /**
             *  设置监听
             */
            consumer.registerMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                    for (MessageExt msg : msgs) {
                        MQConsumer.this.dealBody(msg);
                    }
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });
            consumer.start();
            log.info("---------MQConsumer启动完成------------");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("---------MQConsumer初始化失败------------");
        }
    }

    private void dealBody(MessageExt messageExt) {
        String data = new String(messageExt.getBody());
        log.info("收到MQ的消息为-" + data);
        OaCOrderPersonal personal = JSON.parseObject(data, OaCOrderPersonal.class);
        if (personal == null) {
            return;
        }
        EOrderChannelType channelType = EOrderChannelType.findById(personal.getOrderChannel());
        switch (channelType) {
            /*case didi: {
            }
            break;*/
            case tongcheng: {
                tcOrderStatusSyncService.bindDefaultDriver(personal);
            }
            break;
            case xiecheng: {
                //xcOrderStatusSyncService.bindDefaultDriver(personal);  订单确认取消改用下面的司机确认接口
                xcOrderStatusSyncService.bindDefaultDriverNew(personal);
            }
            break;
            default: {
                log.info("未知平台");
            }
        }
    }
}
